package com.changgou.util;

import com.file.FastDFSFile;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

/**
 * 封装FastDFS的api工具类
 */
public class FastDFSClient {

    /**
     * 初始化FastDFS
     */
    static {
        String path = "fdfs_client.conf";
        String config_name = new ClassPathResource(path).getPath();
        try {
            ClientGlobal.init(config_name);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 文件上传
     *
     * @param file
     * @return
     */
    public static String[] uploadFile(FastDFSFile file) {
        try {
            //获取文件属性
            byte[] file_buff = file.getContent();
            String ext_name = file.getExt();
            NameValuePair[] meta_list = new NameValuePair[1];
            meta_list[0] = new NameValuePair(file.getMd5());

            //1、创建跟踪服务器客户端
            TrackerClient trackerClient = new TrackerClient();
            //2、获取跟踪服务器
            TrackerServer trackerServer = trackerClient.getConnection();
            //3、创建存储服务器客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            //4、文件上传                                    附件扩展名称  附件扩展名称 附件得备注（null）
            String[] uploadResult = storageClient.upload_file(file_buff, ext_name, meta_list);
            return uploadResult;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 文件下载
     *
     * @param group_name
     * @param remote_filename
     * @return
     */
    public static byte[] downloadFile(String group_name, String remote_filename) {
        try {
            //1、创建跟踪服务器得客户端
            TrackerClient trackerClient = new TrackerClient();
            //2、通过跟踪服务器得客户端获取服务器端
            TrackerServer trackerServer = null;

            trackerServer = trackerClient.getConnection();

            //3、创建存储服务器得客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            // 4、附件下载
            byte[] content = storageClient.download_file(group_name, remote_filename);
            return content;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 文件删除
     *
     * @param group_name
     * @param remote_filename
     */
    public static void deleteFile(String group_name, String remote_filename) {
        try {
            //1、创建跟踪服务器得客户端
            TrackerClient trackerClient = new TrackerClient();
            //2、通过跟踪服务器得客户端获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            //3、创建存储服务器得客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            //附件删除
            storageClient.delete_file(group_name, remote_filename);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 附件信息
     * @param group_name
     * @param remote_filename
     * @return
     */
    public static FileInfo getFileInfo(String group_name, String remote_filename) {
        try {
            //1、创建跟踪服务器得客户端
            TrackerClient trackerClient = new TrackerClient();
            //2、通过跟踪服务得客户端获取服务器端
            TrackerServer trackerServer = trackerClient.getConnection();
            //3、创建存储服务器得客户端
            StorageClient storageClient = new StorageClient(trackerServer, null);
            //4、获取文件信息
            FileInfo fileinfo = storageClient.get_file_info(group_name, remote_filename);
            return fileinfo;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 获取tracker服务器地址
     *
     * @return
     */
    public static String getUrl() {
        try {
            //1、创建跟踪服务客户端
            TrackerClient trackerClient = new TrackerClient();
            //2、获取跟踪服务器
            TrackerServer trackerServer = trackerClient.getConnection();
            //3、获取跟踪服务地址
            String ip = trackerServer.getInetSocketAddress().getAddress().getHostAddress();
            int port = ClientGlobal.getG_tracker_http_port();
            String url = "http://" + ip + ":" + port;
            return url;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}



